class AlbumsController < ApplicationController
  before_filter :require_admin,           :only => [:new,   :create,  :edit,    :update,  :destroy]
  before_filter :find_album,              :only => [:show,  :edit,    :update,  :destroy]
  before_filter :find_list_items,         :only => [:show,  :new,     :update,  :create,  :edit]  
  
  def index
    @albums = Album.find(:all, :order => 'created_at DESC')
  end
  
  def show
    @comment = Comment.new
  end
  
  def new
    @album = Album.new
  end
  
  def create
    @album = Album.new(params[:album])
    @album.save!
    flash[:update] = 'The album has been added'
    redirect_to album_url(@album)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = "Oops, that didn't work, see below."
    render :action => 'new'
  end
  
  def edit
  end
  
  def update
    @album.update_attributes!(params[:album])
    flash[:update] = 'The album information has been updated.'
    redirect_to album_url(@album)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = "Oops, that didn't work, see below."
    render :action => 'edit'
  end
  
  def destroy
    @album.destroy
    flash[:update] = 'The photo has been deleted.'
    redirect_to albums_url
  end
  
protected
  def require_login
    unless logged_in?
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def require_admin
    if logged_in?
      unless current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_album
    @album = Album.find params[:id]
  end
  def find_list_items
    @songs    = Song.find     :all, :order => 'created_at desc'
    @shows    = Show.find     :all, :order => 'created_at desc'
    @artworks = Artwork.find  :all, :order => 'created_at desc', :conditions => ['thumbnail IS ?', nil]
    @photos   = Video.find    :all, :order => 'created_at desc', :conditions => ['thumbnail IS ?', nil]
    @videos   = Video.find    :all, :order => 'created_at desc'
    @users    = Video.find    :all, :order => 'created_at desc'
  end
end
